<template>
  <div class="app-container">
    <!-- 添加或修改数字权益卡购卡账单查询对话框 -->
    <el-dialog :title="title" :visible.sync="visible" width="800px" v-if="visible"  append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="disabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="发行方商家id 空为平台" label-width="100px" prop="merchantId">
              <el-input-number v-model="form.merchantId" placeholder="请输入发行方商家id 空为平台" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="关联单号;购卡订单 2 用户消费订单或卡使用记录id)" label-width="100px" prop="relevancyNo">
              <el-input v-model="form.relevancyNo" placeholder="请输入关联单号;购卡订单 2 用户消费订单或卡使用记录id)" maxlength="50" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户id" label-width="100px" prop="customerId">
              <el-input-number v-model="form.customerId" placeholder="请输入客户id" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户卡id" label-width="100px" prop="customerCardId">
              <el-input-number v-model="form.customerCardId" placeholder="请输入用户卡id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单金额" label-width="100px" prop="orderMoney">
              <el-input-number v-model="form.orderMoney" placeholder="请输入订单金额" :precision="2"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="优惠金额" label-width="100px" prop="discountsMoney">
              <el-input-number v-model="form.discountsMoney" placeholder="请输入优惠金额" :precision="2"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易时间" label-width="100px" prop="transactionTime">
                <el-date-picker clearable size="small"
                                v-model="form.transactionTime"
                                type="datetime"
                                format="yyyy-MM-dd HH:mm:ss"
                                placeholder="选择交易时间">
                </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!disabled">确 定</el-button>
        <el-button @click="cancel" v-if="!disabled">取 消</el-button>
        <el-button @click="cancel" v-if="disabled">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { getEquityCardBill, addEquityCardBill, updateEquityCardBill } from "@/api/fundsManager/equityCardBill";

export default {
  name: "Dialog",
  data() {
    return {
      // 是否禁用表单
      disabled: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      visible: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
        merchantId: [
          { required: true, message: "发行方商家id 空为平台不能为空", trigger: "blur" }
        ],
        billType: [
          { required: true, message: "交易明细类型不能为空", trigger: "change" }
        ],
        relevancyNo: [
          { required: true, message: "关联单号;购卡订单 2 用户消费订单或卡使用记录id)不能为空", trigger: "blur" }
        ],
        customerId: [
          { required: true, message: "客户id不能为空", trigger: "blur" }
        ],
        customerCardId: [
          { required: true, message: "用户卡id不能为空", trigger: "blur" }
        ],
        orderMoney: [
          { required: true, message: "订单金额不能为空", trigger: "blur" }
        ],
        discountsMoney: [
          { required: true, message: "优惠金额不能为空", trigger: "blur" }
        ],
        transactionTime: [
          { required: true, message: "交易时间不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {

  },
  methods: {
    /** 打开数字权益卡购卡账单查询弹框 */
    open(id, disabled) {
      this.reset();
      this.disabled = disabled
      if (id) {
        getEquityCardBill(id).then(response => {
          this.form = response.data;
        });
      }
      this.visible = true
      if (this.disabled) {
        this.title = "详情"
      } else if(id) {
        this.title = "修改"
      } else {
        this.title = "新增"
      }
    },
    // 取消按钮
    cancel() {
      this.visible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // 子表选中数据
      this.checked = [];
      // 是否禁用表单
      this.disabled = false;
      // 子表格数据
      this.List = [];
      // 弹出层标题
      this.title = "";
      // 是否显示弹出层
      this.visible = false;
      // 表单参数
      this.form = {};
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 打开遮罩层
          this.$modal.loading("正在处理中数据，请稍后...");
          if (this.form.id != null) {
            updateEquityCardBill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.cancel();
              this.$emit('refresh')
            }).finally(() => {
                // 关闭遮罩层
                this.$modal.closeLoading();
            });
          } else {
            addEquityCardBill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.cancel();
              this.$emit('refresh')
            }).finally(() => {
                // 关闭遮罩层
                this.$modal.closeLoading();
            });
          }
        }
      });
    },
    /** 序号 */
    rowIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 添加按钮操作 */
    handleAdd() {
      let obj = {};
      this.List.push(obj);
    },
    /** 删除按钮操作 */
    handleDelete() {
      if (this.checked.length == 0) {
        this.$modal.msgError("请先选择要删除的数据");
      } else {
        const List = this.List;
        const checked = this.checked;
        this.List = List.filter(function(item) {
          return checked.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleSelectionChange(selection) {
      this.checked = selection.map(item => item.index)
    },
  }
};
</script>
